import StringIO
import logging
from google.appengine.api import urlfetch
from google.appengine.api import search
from google.appengine.ext import ndb
import webapp2
from parsefeed import get_item_list
import data

class UpdateAll(webapp2.RequestHandler):
    def get(self):
        q = data.Subscription.query(
                default_options=ndb.QueryOptions(keys_only=True))\
                .filter(data.Subscription.enabled == True)
        tasks = []
        for key in q:
            rpc = urlfetch.create_rpc()
            urlfetch.make_fetch_call(rpc, key.id())
            tasks.append((rpc, key))
        futurelist = []
        index = search.Index(name='ItemIndex')
        d = []
        for task in tasks:
            try:
                result = task[0].get_result()
                if result.status_code == 200:
                    f = StringIO.StringIO(result.content)
                else:
                    raise urlfetch.DownloadError
            except urlfetch.Error:
                logging.error('Error downloading feed %s', task[1].id())
                continue
            try:
                items = get_item_list(f)
            except:
                logging.error('Error parsing feed %s', task[1].id())
                continue
            e = []
            for item in reversed(items):
                if data.Item.get_by_id(item['guid']) == None:
                    e.append(data.Item(
                            key=ndb.Key('Item', item['guid']),
                            title=item['title'],
                            link=item['link'],
                            description=item['description'],
                            sub=ndb.Key('Subscription', task[1].id())))
                    d.append(search.Document(
                        doc_id=item['guid'],
                        fields=[
                            search.TextField(name='title',
                                             value=item['title']),
                            search.HtmlField(name='description',
                                             value=item['description']),
                            search.AtomField(name='starred',
                                               value='False'),
                            search.AtomField(name='subscription',
                                             value=task[1].id())
                            ]
                        ))
            if e:
                sub = data.Subscription.get_by_id(task[1].id())
                sub.count += len(e)
                e.append(sub)
                futurelist.extend(ndb.put_multi_async(e))
        for seg in [d[i:i+200] for i in range(0, len(d), 200)]:
            index.put(seg)
        for future in futurelist:
            future.get_result()


update = webapp2.WSGIApplication([('/update', UpdateAll)], debug=True)
